package de.szut.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.szut.entity.Country;
import de.szut.logic.Logic;
import de.szut.splasher.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nbothmer
 * Date: 15.10.13
 * Time: 09:43
 * To change this template use File | Settings | File Templates.
 */
public class SpreadingTabFragment extends Fragment {


    private ListView nonInfectedListView;
    private ListView infectedListView;
    private ListView controlledListView;
    private List<Country> nonInfectedCountries = new ArrayList<Country>();
    private List<Country> infectedCountries= new ArrayList<Country>();
    private List<Country> controlledCountries= new ArrayList<Country>();
    private NonInfectedAdapter nonInfectedAdapter;
    private InfectedAdapter infectedAdapter;
    private ControlledAdapter controlledAdapter;
    private Logic logic;

    public SpreadingTabFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        logic = Logic.getInstance(getActivity());
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.spreading_tab_view, container, false);
        nonInfectedListView = (ListView) root.findViewById(R.id.nonInfected_listview);
        infectedListView = (ListView) root.findViewById(R.id.infected_listview);
        controlledListView = (ListView) root.findViewById(R.id.controlled_listview);
        setValues();
        nonInfectedAdapter = new NonInfectedAdapter(getActivity(), 0, nonInfectedCountries);
        infectedAdapter = new InfectedAdapter(getActivity(), 0, infectedCountries);
        controlledAdapter = new ControlledAdapter(getActivity(), 0, controlledCountries);
        nonInfectedListView.setAdapter(nonInfectedAdapter);
        infectedListView.setAdapter(infectedAdapter);
        controlledListView.setAdapter(controlledAdapter);
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        setValues();
        infectedAdapter.notifyDataSetChanged();
        nonInfectedAdapter.notifyDataSetChanged();
        controlledAdapter.notifyDataSetChanged();
    }

    public void setValues() {
        resetValue();
        nonInfectedCountries.addAll(logic.getNonInfectedCountries());
        infectedCountries.addAll(logic.getInfectedCountries());
        controlledCountries.addAll(logic.getControlledCountries());

    }
    private void resetValue(){
        nonInfectedCountries.clear();
        infectedCountries.clear();
        controlledCountries.clear();
    }

    public class NonInfectedAdapter extends ArrayAdapter<Country> {

        List<Country> countries;

        public NonInfectedAdapter(Context context, int textViewResourceId, List<Country> countries) {
            super(context, textViewResourceId, countries);
            this.countries = countries;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ItemView itemView = new ItemView();
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.spreading_tab_item, parent, false);
//                itemView.infected = (TextView) v.findViewById(R.id.infected_country);
                itemView.countryView = (TextView) v.findViewById(R.id.non_infected_country);
//                itemView.dead = (TextView) v.findViewById(R.id.death_country);
                v.setTag(itemView);
            } else {
                itemView = (ItemView) v.getTag();
            }
            if (countries.size() > 0) {
                Country c = countries.get(position);
//            int infected=0;
//            int death=0;
                if (c != null) {
                    itemView.countryView.setText(c.getName());
//                for (OperatingSystem os : c.getOperatingSystems()) {
//                    if (os.getUsers().getControlledUsers() == os.getUsers().getTotalUsers()) {
//                        death++;
//                    }else if (os.getUsers().getInfectedUsers() >0) {
//                        infected++;
//                    }
//                }
//                if(infected >0){
//                    itemView.infected.setText(c.getName());
//                    itemView.dead.setText("-");
//                }else if(death >0){
//                    itemView.dead.setText(c.getName());
//                }else if(infected == 0 && death == 0){
//                    itemView.nonInfected.setText(c.getName());
//                    itemView.dead.setText("-");
//                    itemView.infected.setText("-");
//                }
                }
            } else {
                itemView.countryView.setText("-");
            }
            return v;
        }

        private class ItemView {
            TextView countryView;
//            TextView infected;
//            TextView dead;
        }
    }

    public class InfectedAdapter extends ArrayAdapter<Country> {

        List<Country> countries;

        public InfectedAdapter(Context context, int textViewResourceId, List<Country> countries) {
            super(context, textViewResourceId, countries);
            this.countries = countries;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ItemView itemView = new ItemView();
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.spreading_tab_item, parent, false);
//                itemView.infected = (TextView) v.findViewById(R.id.infected_country);
                itemView.countryView = (TextView) v.findViewById(R.id.non_infected_country);
//                itemView.dead = (TextView) v.findViewById(R.id.death_country);
                v.setTag(itemView);
            } else {
                itemView = (ItemView) v.getTag();
            }
            if (countries.size() > 0) {
                Country c = countries.get(position);
//            int infected=0;
//            int death=0;
                if (c != null) {
                    itemView.countryView.setText(c.getName());
//                for (OperatingSystem os : c.getOperatingSystems()) {
//                    if (os.getUsers().getControlledUsers() == os.getUsers().getTotalUsers()) {
//                        death++;
//                    }else if (os.getUsers().getInfectedUsers() >0) {
//                        infected++;
//                    }
//                }
//                if(infected >0){
//                    itemView.infected.setText(c.getName());
//                    itemView.dead.setText("-");
//                }else if(death >0){
//                    itemView.dead.setText(c.getName());
//                }else if(infected == 0 && death == 0){
//                    itemView.nonInfected.setText(c.getName());
//                    itemView.dead.setText("-");
//                    itemView.infected.setText("-");
//                }
                }
            } else {
                itemView.countryView.setText("-");
            }            return v;
        }

        private class ItemView {
            TextView countryView;
//            TextView infected;
//            TextView dead;
        }
    }

    public class ControlledAdapter extends ArrayAdapter<Country> {

        List<Country> countries;

        public ControlledAdapter(Context context, int textViewResourceId, List<Country> countries) {
            super(context, textViewResourceId, countries);
            this.countries = countries;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ItemView itemView = new ItemView();
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.spreading_tab_item, parent, false);
//                itemView.infected = (TextView) v.findViewById(R.id.infected_country);
                itemView.countryView = (TextView) v.findViewById(R.id.non_infected_country);
//                itemView.dead = (TextView) v.findViewById(R.id.death_country);
                v.setTag(itemView);
            } else {
                itemView = (ItemView) v.getTag();
            }
            if (countries.size() > 0) {
                Country c = countries.get(position);
//            int infected=0;
//            int death=0;
                if (c != null) {
                    itemView.countryView.setText(c.getName());
//                for (OperatingSystem os : c.getOperatingSystems()) {
//                    if (os.getUsers().getControlledUsers() == os.getUsers().getTotalUsers()) {
//                        death++;
//                    }else if (os.getUsers().getInfectedUsers() >0) {
//                        infected++;
//                    }
//                }
//                if(infected >0){
//                    itemView.infected.setText(c.getName());
//                    itemView.dead.setText("-");
//                }else if(death >0){
//                    itemView.dead.setText(c.getName());
//                }else if(infected == 0 && death == 0){
//                    itemView.nonInfected.setText(c.getName());
//                    itemView.dead.setText("-");
//                    itemView.infected.setText("-");
//                }
                }
            } else {
                itemView.countryView.setText("-");
            }
            return v;
        }

        private class ItemView {
            TextView countryView;
//            TextView infected;
//            TextView dead;
        }
    }
}
